package com.guichun.insert;

import com.guichun.Jdbcs;

import java.sql.*;

/**
 * @author guichun
 * @version 1.0.0
 * @ClassName JdbcInV2.java
 * @email onebigbai@icloud.com
 * @Description JDBC新增数据V2 有问题
 * @createTime 2021-03-09
 */
public class JdbcInV2 {

    public  JdbcInV2(Jdbcs jdbcs) {
        try {
            Connection connection = DriverManager.getConnection(jdbcs.getJdbcUrl(), jdbcs.getJdbcRoot(), jdbcs.getJdbcPassword());

            PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO students (grade, name, gender,)VALUES (?,?,?)");

            preparedStatement.setObject(1,1);
            preparedStatement.setObject(2,"bai");
            preparedStatement.setObject(3,0);

            int executeUpdate = preparedStatement.executeUpdate();
            System.out.println(executeUpdate);

            ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
            if (generatedKeys.next()) {
                long id = generatedKeys.getLong(1);
            }

        }catch (SQLException e) {
            System.out.println("e = " + e);
        }
    }
}
